/****************************************************************************
 *
 * Copyright (C) 2003-2010 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.dataset.netflow;

import java.sql.Timestamp;

import nfse.NetFSERecord;
import nfse.NetFSERecordFactory;

public class NetflowV2RecordFactory extends NetFSERecordFactory {

    @Override
    public NetFSERecord generate(String line, int sensor) {
        try {

            NetflowV2Record l = new NetflowV2Record();
            l.setInitialString(line);
            l.version = 2;
            l.setSensorID(sensor);
            String[] tokens = line.split(",");

            l.setSrcIP(Long.parseLong(tokens[0]));
            l.setDstIP(Long.parseLong(tokens[1]));

            l.srcPort = Integer.parseInt(tokens[6]);
            l.dstPort = Integer.parseInt(tokens[7]);
            l.srcPkts = Long.parseLong(tokens[10]);
            l.srcBytes = Long.parseLong(tokens[8]);

            l.proto = Integer.parseInt(tokens[5]);

            Timestamp ts = null;
            try {
                ts = Timestamp.valueOf(tokens[9]);
            } catch (Exception e) {
                // try to see if timestamp is YYYYMMDDHHMMSS
                String temp = tokens[9].substring(0, 4) + "-" + tokens[9].substring(4, 6) + "-"
                        + tokens[9].substring(6, 8) + " " + tokens[9].substring(8, 10) + ":"
                        + tokens[9].substring(10, 12) + ":" + tokens[9].substring(12);
                try {
                    ts = Timestamp.valueOf(temp);
                } catch (Exception e2) {
                    ts = new Timestamp(System.currentTimeMillis());
                }
            }

            l.setStartTS(ts);

            l.tos = Integer.parseInt(tokens[13]);
            l.tcpFlags = tokens[14];

            return l; // The record was sucessfully parsed, return it
        } catch (Exception E) {
            E.printStackTrace();
        }
        return null; // Return null if there is an error in parsing
    }

    @Override
    public NetFSERecord generate() {
        return new NetflowV2Record();
    }

}
